package com.eagledata.project.business.weeklyInfo.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.util.http.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.eagledata.common.exception.file.FileNameLengthLimitExceededException;
import com.eagledata.common.utils.FileUploadUtils;
import com.eagledata.framework.config.EagleDataConfig;
import com.eagledata.framework.web.controller.BaseController;
import com.eagledata.framework.web.domain.Message;
import com.eagledata.framework.web.page.TableDataInfo;
import com.eagledata.project.business.weeklyInfo.domain.WeeklyInfo;
import com.eagledata.project.business.weeklyInfo.service.IWeeklyInfoService;
import com.eagledata.project.system.dict.cache.DictCacheManager;

/**
 * M5周刊 信息操作处理
 * 
 * @author eagledata
 * @date 2018-06-25
 */
@Controller
@RequestMapping("/business/weeklyInfo")
public class WeeklyInfoController extends BaseController {
    private String prefix = "business/weeklyInfo";
	
	@Autowired
	private IWeeklyInfoService weeklyInfoService;
	
	@RequestMapping()
	@RequiresPermissions("business:weeklyInfo:view")
	public String weeklyInfo(WeeklyInfo weeklyInfo, Model model) {
		model.addAttribute("weeklyInfo", weeklyInfo);
	    return prefix + "/weeklyInfo";
	}
	
	/**
	 * 查询M5周刊列表
	 */
	@RequiresPermissions("business:weeklyInfo:list")
	@RequestMapping("/list")
	@ResponseBody
	public TableDataInfo list(WeeklyInfo weeklyInfo, Model model) {
		startPage();
        List<WeeklyInfo> list = weeklyInfoService.selectWeeklyInfoList(weeklyInfo);
        model.addAttribute("weeklyInfo", weeklyInfo);
		return getDataTable(list);
	}
	
	/**
	 * 新增M5周刊
	 */
	@RequiresPermissions("business:weeklyInfo:add")
	@RequestMapping("/add")
	public String add(WeeklyInfo weeklyInfo, Model model) {
		model.addAttribute("weeklyInfo", weeklyInfo);
	    return prefix + "/add";
	}

	/**
	 * 修改M5周刊
	 */
	@RequiresPermissions("business:weeklyInfo:edit")
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id,WeeklyInfo weekly, Model model) {
		WeeklyInfo weeklyInfo = weeklyInfoService.selectWeeklyInfoById(id);
		if(null!=weeklyInfo&&StringUtils.isNotBlank(weekly.getWeeklyType())) {
			 String dictKey = "";
			 if("article".equals(weekly.getWeeklyType())){
				 dictKey = "m_Information_type";
			 }else if("legend".equals(weekly.getWeeklyType())) {
				 dictKey = "m_legend_type";
			 }
			 Map<String, String> dictData = DictCacheManager.getInstance().getDictData(dictKey);
			 model.addAttribute("dictData", dictData);
			 model.addAttribute("weeklyInfo", weeklyInfo);
		}
	    return prefix + "/edit";
	}
	
	/**
	 * 保存M5周刊
	 */
	@RequiresPermissions("business:weeklyInfo:save")
	@RequestMapping("/save")
	@ResponseBody
	public Message save(MultipartFile file, WeeklyInfo weeklyInfo ) {
		if(null!=file) {
			try {
				String baseDir = EagleDataConfig.getProfile();
				String extension = FileUploadUtils.getFileSuffix(file.getOriginalFilename());
				String fileName = FileUploadUtils.upload(baseDir, file, extension);
				weeklyInfo.setImageUrl(fileName);
			} catch (FileSizeLimitExceededException e) {
				e.printStackTrace();
			} catch (FileNameLengthLimitExceededException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (weeklyInfoService.saveWeeklyInfo(weeklyInfo) > 0)
		{
			return Message.success();
		}
		return Message.error();
	}
	
	/**
	 * 删除M5周刊
	 */
	@RequiresPermissions("business:weeklyInfo:remove")
	@RequestMapping( "/remove/{id}")
	@ResponseBody
	public Message remove(@PathVariable("id") Integer id) {
		if (weeklyInfoService.deleteWeeklyInfoById(id) > 0)
		{
		    return Message.success();
		}
		return Message.error();
	}
	
	/**
	 * 批量删除M5周刊
	 */
	@RequiresPermissions("business:weeklyInfo:batchRemove")
	@RequestMapping( "/batchRemove")
	@ResponseBody
	public Message remove(@RequestParam("ids[]") Integer[] ids) {
		int rows = weeklyInfoService.batchDeleteWeeklyInfo(ids);
		if (rows > 0)
        {
            return Message.success();
        }
        return Message.error();
	}
	
	/**
	 * 接收KindEditor上传文件
	 * @param file
	 * @return
	 */
	@RequestMapping( "/uploadFile")
	@ResponseBody
	public JSONObject uploadFile(MultipartFile imgFile) {
		JSONObject result = new JSONObject();
		result.put("error", 1);
		if(null!=imgFile) {
			String imageUrl = "";
			try {
				String baseDir = EagleDataConfig.getProfile();
				String extension = FileUploadUtils.getFileSuffix(imgFile.getOriginalFilename());
				String fileName = FileUploadUtils.upload(baseDir, imgFile, extension);
				imageUrl = StringUtils.join(EagleDataConfig.getBaseImgUrl(),fileName);
				result.put("error", 0);
				result.put("url", imageUrl);
			} catch (Exception e) {
				result.put("error", 1);
				result.put("url", imageUrl);
				e.printStackTrace();
			}
		}
		return result;
	}
}
